home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / BCHDMP.C < prev    next >
C/C++ Source or Header  |  1992-06-03  |  25KB  |  914 lines

  1. /* -*-C-*-
  2.  
  3. $Header: /scheme/src/microcode/RCS/bchdmp.c,v 9.65 1992/06/03 21:54:18 jinx Exp $
  4.  
  5. Copyright (c) 1987-1992 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. /* bchgcl, bchmmg, bchpur, and bchdmp can replace gcloop, memmag,
  36.    purify, and fasdump, respectively, to provide garbage collection
  37.    and related utilities to disk. */
  38.  
  39. #include "scheme.h"
  40. #include "prims.h"
  41. #include "osfile.h"
  42. #include "trap.h"
  43. #include "lookup.h"        /* UNCOMPILED_VARIABLE */
  44. #define In_Fasdump
  45. #include "fasl.h"
  46.  
  47. #ifdef DOS386
  48. #  include "msdos.h"
  49. #  include "dosio.h"
  50.  
  51. char *
  52. DEFUN (mktemp, (fname), unsigned char * fname)
  53. {
  54.   /* Should call tmpname */
  55.  
  56.   return;
  57. }
  58.  
  59. #  define FASDUMP_FILENAME "\\tmp\\fasdump.bin"
  60.  
  61. #else /* not DOS386 */
  62.  
  63. #  include "ux.h"
  64. #  include "uxio.h"
  65. extern int EXFUN (unlink, (CONST char *));
  66.  
  67. #  define FASDUMP_FILENAME "/tmp/fasdumpXXXXXX"
  68.  
  69. #if defined(_HPUX) || defined(_SUNOS4) || defined(_ULTRIX) || defined(_SYSV4)
  70. #  define HAVE_FTRUNCATE
  71. #endif
  72.  
  73. #if (defined(_HPUX) && (_HPUX_VERSION >= 80)) || defined(_SYSV4)
  74. #  define FTRUNCATE_DECLARED
  75. #endif
  76.  
  77. #endif /* DOS386 */
  78.  
  79. #include "bchgcc.h"
  80.  
  81. static Tchannel dump_channel;
  82.  
  83. #define Write_Data(size, buffer)                    \
  84.   ((OS_channel_write_dump_file                        \
  85.     (dump_channel,                            \
  86.      ((char *) (buffer)),                        \
  87.      ((size) * (sizeof (SCHEME_OBJECT)))))                \
  88.    / (sizeof (SCHEME_OBJECT)))
  89.  
  90. #include "dump.c"
  91.  
  92. extern SCHEME_OBJECT
  93.   EXFUN (dump_renumber_primitive, (SCHEME_OBJECT)),
  94.   * EXFUN (initialize_primitive_table, (SCHEME_OBJECT *, SCHEME_OBJECT *)),
  95.   * EXFUN (cons_primitive_table, (SCHEME_OBJECT *, SCHEME_OBJECT *, long *)),
  96.   * EXFUN (cons_whole_primitive_table,
  97.        (SCHEME_OBJECT *, SCHEME_OBJECT *, long *));
  98.  
  99. static char *dump_file_name;
  100. static int real_gc_file, dump_file;
  101. static SCHEME_OBJECT *saved_free;
  102. static SCHEME_OBJECT *fixup_buffer = ((SCHEME_OBJECT *) NULL);
  103. static SCHEME_OBJECT *fixup_buffer_end;
  104. static SCHEME_OBJECT *fixup;
  105. static fixup_count = 0;
  106. static Boolean compiled_code_present_p;
  107.  
  108. /* Utility macros. */
  109.  
  110. #define fasdump_remember_to_fix(location, contents)            \
  111. {                                    \
  112.   if ((fixup == fixup_buffer) && (!(reset_fixes ())))            \
  113.   {                                    \
  114.     return (PRIM_INTERRUPT);                        \
  115.   }                                    \
  116.   *--fixup = contents;                            \
  117.   *--fixup = ((SCHEME_OBJECT) location);                \
  118. }
  119.  
  120. #define fasdump_normal_setup()                        \
  121. {                                    \
  122.   Old = (OBJECT_ADDRESS (Temp));                    \
  123.   if ((OBJECT_TYPE (*Old)) == TC_BROKEN_HEART)                \
  124.   {                                    \
  125.     *Scan = (MAKE_OBJECT_FROM_OBJECTS (Temp, *Old));            \
  126.     continue;                                \
  127.   }                                    \
  128.   New_Address = (MAKE_BROKEN_HEART (To_Address));            \
  129.   fasdump_remember_to_fix (Old, *Old);                    \
  130. }
  131.  
  132. #ifdef FLOATING_ALIGNMENT
  133.  
  134. #define fasdump_flonum_setup()                        \
  135. {                                    \
  136.   Old = (OBJECT_ADDRESS (Temp));                    \
  137.   if ((OBJECT_TYPE (*Old)) == TC_BROKEN_HEART)                \
  138.   {                                    \
  139.     *Scan = (MAKE_OBJECT_FROM_OBJECTS (Temp, *Old));            \
  140.     continue;                                \
  141.   }                                    \
  142.   FLOAT_ALIGN_FREE (To_Address, To);                    \
  143.   New_Address = (MAKE_BROKEN_HEART (To_Address));            \
  144.   fasdump_remember_to_fix (Old, *Old);                    \
  145. }
  146.  
  147. #else /* FLOATING_ALIGNMENT */
  148.  
  149. #define fasdump_flonum_setup()    fasdump_normal_setup ()
  150.  
  151. #endif /* FLOATING_ALIGNMENT */
  152.  
  153. #define fasdump_transport_end(length)                    \
  154. {                                    \
  155.   To_Address += (length);                        \
  156.   if (To >= free_buffer_top)                        \
  157.   {                                    \
  158.     To = (dump_and_reset_free_buffer ((To - free_buffer_top),        \
  159.                       &success));            \
  160.     if (!success)                            \
  161.     {                                    \
  162.       return (PRIM_INTERRUPT);                        \
  163.     }                                    \
  164.   }                                    \
  165. }
  166.  
  167. #define fasdump_normal_transport(copy_code, length)            \
  168. {                                    \
  169.   copy_code;                                \
  170.   fasdump_transport_end (length);                    \
  171. }
  172.  
  173. #define fasdump_normal_end()                        \
  174. {                                    \
  175.   *(OBJECT_ADDRESS (Temp)) = New_Address;                \
  176.   *Scan = (MAKE_OBJECT_FROM_OBJECTS (Temp, New_Address));        \
  177.   continue;                                \
  178. }
  179.  
  180. #define fasdump_normal_pointer(copy_code, length)            \
  181. {                                    \
  182.   fasdump_normal_setup ();                        \
  183.   fasdump_normal_transport (copy_code, length);                \
  184.   fasdump_normal_end ();                        \
  185. }
  186.  
  187. #define fasdump_typeless_setup()                    \
  188. {                                    \
  189.   Old = ((SCHEME_OBJECT *) Temp);                    \
  190.   if (OBJECT_TYPE (*Old) == TC_BROKEN_HEART)                \
  191.   {                                    \
  192.     *Scan = ((SCHEME_OBJECT) OBJECT_ADDRESS (*Old));            \
  193.     continue;                                \
  194.   }                                    \
  195.   New_Address = ((SCHEME_OBJECT) To_Address);                \
  196.   fasdump_remember_to_fix (Old, *Old);                    \
  197. }
  198.  
  199. #define fasdump_typeless_end()                        \
  200. {                                    \
  201.   (* (OBJECT_ADDRESS (Temp))) = (MAKE_BROKEN_HEART (New_Address));    \
  202.   *Scan = ((SCHEME_OBJECT) New_Address);                \
  203.   continue;                                \
  204. }
  205.  
  206. #define fasdump_typeless_pointer(copy_code, length)            \
  207. {                                    \
  208.   fasdump_typeless_setup ();                        \
  209.   fasdump_normal_transport (copy_code, length);                \
  210.   fasdump_typeless_end ();                        \
  211. }
  212.  
  213. #define fasdump_compiled_entry()                    \
  214. do {                                    \
  215.   compiled_code_present_p = true;                    \
  216.   Old = OBJECT_ADDRESS (Temp);                        \
  217.   Compiled_BH (false, continue);                    \
  218.   {                                    \
  219.     SCHEME_OBJECT *Saved_Old = Old;                    \
  220.                                     \
  221.     fasdump_remember_to_fix (Old, *Old);                \
  222.     New_Address = (MAKE_BROKEN_HEART (To_Address));            \
  223.     copy_vector (&success);                        \
  224.     if (!success)                            \
  225.     {                                    \
  226.       return (PRIM_INTERRUPT);                        \
  227.     }                                    \
  228.     *Saved_Old = New_Address;                        \
  229.     Temp = RELOCATE_COMPILED (Temp, (OBJECT_ADDRESS (New_Address)),    \
  230.                   Saved_Old);                \
  231.     continue;                                \
  232.   }                                    \
  233. } while (false)
  234.  
  235. #define fasdump_linked_operator()                    \
  236. {                                    \
  237.   Scan = ((SCHEME_OBJECT *) (word_ptr));                \
  238.   BCH_EXTRACT_OPERATOR_LINKAGE_ADDRESS (Temp, Scan);            \
  239.   fasdump_compiled_entry ();                        \
  240.   BCH_STORE_OPERATOR_LINKAGE_ADDRESS (Temp, Scan);            \
  241. }
  242.  
  243. #define fasdump_manifest_closure()                    \
  244. {                                    \
  245.   Scan = ((SCHEME_OBJECT *) (word_ptr));                \
  246.   BCH_EXTRACT_CLOSURE_ENTRY_ADDRESS (Temp, Scan);            \
  247.   fasdump_compiled_entry ();                        \
  248.   BCH_STORE_CLOSURE_ENTRY_ADDRESS (Temp, Scan);                \
  249. }
  250.  
  251. Boolean
  252. DEFUN (fasdump_exit, (length), long length)
  253. {
  254.   fast SCHEME_OBJECT *fixes, *fix_address;
  255.   Boolean result;
  256.  
  257.   Free = saved_free;
  258.   restore_gc_file ();
  259.  
  260. #ifdef HAVE_FTRUNCATE
  261.   {
  262. #ifndef FTRUNCATE_DECLARED
  263.     extern int EXFUN (ftruncate, (int, unsigned long));
  264. #endif
  265.     ftruncate (dump_file, length);
  266.     result = ((close (dump_file)) == 0);
  267.   }
  268. #else
  269.  
  270.   result = (close (dump_file) == 0);
  271.  
  272. #endif /* HAVE_FTRUNCATE */
  273. #if defined(HAVE_TRUNCATE) && !defined(HAVE_FTRUNCATE)
  274.   {
  275.     extern int EXFUN (truncate, (CONST char *, unsigned long));
  276.  
  277.     truncate (dump_file_name, length);
  278.   }
  279. #endif /* HAVE_TRUNCATE */
  280.  
  281.   if (length == 0)
  282.   {
  283.     (void) (unlink (dump_file_name));
  284.   }
  285.   dump_file_name = ((char *) NULL);
  286.  
  287.   fixes = fixup;
  288.  
  289. next_buffer:
  290.  
  291.   while (fixes != fixup_buffer_end)
  292.   {
  293.     fix_address = ((SCHEME_OBJECT *) (*fixes++));    /* Where it goes. */
  294.     *fix_address = *fixes++;                /* Put it there. */
  295.   }
  296.  
  297.   if (fixup_count >= 0)
  298.   {
  299. #if defined(_SYSV3) && !defined(_HPUX)
  300.     extern int EXFUN (read, (int, char *, int));
  301. #endif
  302.     if ((retrying_file_operation
  303.      (read, real_gc_file, ((char *) fixup_buffer),
  304.       (gc_file_start_position + (fixup_count << gc_buffer_byte_shift)),
  305.       gc_buffer_bytes, "read", "the fixup buffer",
  306.       &gc_file_current_position, io_error_retry_p))
  307.     != gc_buffer_bytes)
  308.     {
  309.       gc_death (TERM_EXIT,
  310.         "fasdump: Could not read back the fasdump fixup information",
  311.         NULL, NULL);
  312.       /*NOTREACHED*/
  313.     }
  314.     fixup_count -= 1;
  315.     fixes = fixup_buffer;
  316.     goto next_buffer;
  317.   }
  318.  
  319.   fixup = fixes;
  320.   Fasdump_Exit_Hook ();
  321.   return (result);
  322. }
  323.  
  324. Boolean
  325. DEFUN_VOID (reset_fixes)
  326. {
  327. #if defined(_SYSV3) && !defined(_HPUX)
  328.     extern int EXFUN (write, (int, char *, int));
  329. #endif
  330.  
  331.   long start;
  332.  
  333.   fixup_count += 1;
  334.   start = (gc_file_start_position + (fixup_count << gc_buffer_byte_shift));
  335.  
  336.   if (((start + gc_buffer_bytes) > gc_file_end_position)
  337.       || ((retrying_file_operation
  338.        (write, real_gc_file, ((char *) fixup_buffer),
  339.         start, gc_buffer_bytes, "write", "the fixup buffer",
  340.         &gc_file_current_position, io_error_always_abort))
  341.       != gc_buffer_bytes))
  342.     return (false);
  343.   fixup = fixup_buffer_end;
  344.   return (true);
  345. }
  346.  
  347. /* A copy of GCLoop, with minor modifications. */
  348.  
  349. long
  350. DEFUN (dumploop, (Scan, To_ptr, To_Address_ptr),
  351.        fast SCHEME_OBJECT *Scan AND
  352.        SCHEME_OBJECT **To_ptr AND
  353.        SCHEME_OBJECT **To_Address_ptr)
  354. {
  355.   fast SCHEME_OBJECT *To, *Old, Temp, *To_Address, New_Address;
  356.   Boolean success;
  357.  
  358.   success = true;
  359.   To = *To_ptr;
  360.   To_Address = *To_Address_ptr;
  361.  
  362.   for ( ; Scan != To; Scan++)
  363.   {
  364.     Temp = *Scan;
  365.     Switch_by_GC_Type (Temp)
  366.     {
  367.       case TC_BROKEN_HEART:
  368.         if ((OBJECT_DATUM (Temp)) == 0)
  369.     {
  370.       break;
  371.     }
  372.         if (Temp != (MAKE_POINTER_OBJECT (TC_BROKEN_HEART, Scan)))
  373.     {
  374.       sprintf (gc_death_message_buffer,
  375.            "purifyloop: broken heart (0x%lx) in scan",
  376.            Temp);
  377.       gc_death (TERM_BROKEN_HEART, gc_death_message_buffer, Scan, To);
  378.       /*NOTREACHED*/
  379.     }
  380.     if (Scan != scan_buffer_top)
  381.     {
  382.       goto end_dumploop;
  383.     }
  384.  
  385.     /* The -1 is here because of the Scan++ in the for header. */
  386.  
  387.     Scan = ((dump_and_reload_scan_buffer (0, &success)) - 1);
  388.     if (!success)
  389.     {
  390.       return (PRIM_INTERRUPT);
  391.     }
  392.     continue;
  393.  
  394.       case TC_MANIFEST_NM_VECTOR:
  395.       case TC_MANIFEST_SPECIAL_NM_VECTOR:
  396.     /* Check whether this bumps over current buffer,
  397.        and if so we need a new bufferfull. */
  398.     Scan += (OBJECT_DATUM (Temp));
  399.     if (Scan < scan_buffer_top)
  400.     {
  401.       break;
  402.     }
  403.     else
  404.     {
  405.       unsigned long overflow;
  406.  
  407.       /* The + & -1 are here because of the Scan++ in the for header. */
  408.       overflow = ((Scan - scan_buffer_top) + 1);
  409.       Scan = (((dump_and_reload_scan_buffer ((overflow >> gc_buffer_shift),
  410.                          &success)) +
  411.            (overflow & gc_buffer_mask)) - 1);
  412.       if (!success)
  413.       {
  414.         return (PRIM_INTERRUPT);
  415.       }
  416.       break;
  417.     }
  418.  
  419.       case TC_PRIMITIVE:
  420.       case TC_PCOMB0:
  421.     *Scan = (dump_renumber_primitive (*Scan));
  422.     break;
  423.  
  424.       case_compiled_entry_point:
  425.     fasdump_compiled_entry ();
  426.     *Scan = Temp;
  427.     break;
  428.  
  429.       case TC_LINKAGE_SECTION:
  430.       {
  431.     switch (READ_LINKAGE_KIND (Temp))
  432.     {
  433.       case REFERENCE_LINKAGE_KIND:
  434.       case ASSIGNMENT_LINKAGE_KIND:
  435.       {
  436.         /* count typeless pointers to quads follow. */
  437.  
  438.         fast long count;
  439.         long max_count, max_here;
  440.  
  441.         Scan++;
  442.         max_here = (scan_buffer_top - Scan);
  443.         max_count = (READ_CACHE_LINKAGE_COUNT (Temp));
  444.         while (max_count != 0)
  445.         {
  446.           count = ((max_count > max_here) ? max_here : max_count);
  447.           max_count -= count;
  448.           for ( ; --count >= 0; Scan += 1)
  449.           {
  450.         Temp = *Scan;
  451.         fasdump_typeless_pointer (copy_quadruple (), 4);
  452.           }
  453.           if (max_count != 0)
  454.           {
  455.         /* We stopped because we needed to relocate too many. */
  456.         Scan = (dump_and_reload_scan_buffer (0, NULL));
  457.         max_here = gc_buffer_size;
  458.           }
  459.         }
  460.         /* The + & -1 are here because of the Scan++ in the for header. */
  461.         Scan -= 1;
  462.         break;
  463.       }
  464.  
  465.       case OPERATOR_LINKAGE_KIND:
  466.       case GLOBAL_OPERATOR_LINKAGE_KIND:
  467.       {
  468.         /* Operator linkage */
  469.  
  470.         fast long count;
  471.         fast char *word_ptr, *next_ptr;
  472.         long overflow;
  473.  
  474.         word_ptr = (FIRST_OPERATOR_LINKAGE_ENTRY (Scan));
  475.         if (word_ptr > ((char *) scan_buffer_top))
  476.         {
  477.           overflow = (word_ptr - ((char *) Scan));
  478.           extend_scan_buffer (word_ptr, To);
  479.           BCH_START_OPERATOR_RELOCATION (Scan);
  480.           word_ptr = (end_scan_buffer_extension (word_ptr));
  481.           Scan = ((SCHEME_OBJECT *) (word_ptr - overflow));
  482.         }
  483.         else
  484.           BCH_START_OPERATOR_RELOCATION (Scan);
  485.         
  486.         count = (READ_OPERATOR_LINKAGE_COUNT (Temp));
  487.         overflow = ((END_OPERATOR_LINKAGE_AREA (Scan, count)) -
  488.             scan_buffer_top);
  489.  
  490.         for (next_ptr = (NEXT_LINKAGE_OPERATOR_ENTRY (word_ptr));
  491.          (--count >= 0);
  492.          word_ptr = next_ptr,
  493.          next_ptr = (NEXT_LINKAGE_OPERATOR_ENTRY (word_ptr)))
  494.         {
  495.           if (next_ptr > ((char *) scan_buffer_top))
  496.           {
  497.         extend_scan_buffer (next_ptr, To);
  498.         fasdump_linked_operator ();
  499.         next_ptr = (end_scan_buffer_extension (next_ptr));
  500.         overflow -= gc_buffer_size;
  501.           }
  502.           else
  503.         fasdump_linked_operator ();
  504.         }
  505.         Scan = (scan_buffer_top + overflow);
  506.         BCH_END_OPERATOR_RELOCATION (Scan);
  507.         break;
  508.       }
  509.  
  510.       default:
  511.       {
  512.         gc_death (TERM_EXIT,
  513.               "fasdump: Unknown compiler linkage kind.",
  514.               Scan, Free);
  515.         /*NOTREACHED*/
  516.       }
  517.     }
  518.     break;
  519.       }
  520.  
  521.       case TC_MANIFEST_CLOSURE:
  522.       {
  523.     fast long count;
  524.     fast char * word_ptr;
  525.     char * end_ptr;
  526.  
  527.     Scan += 1;
  528.  
  529.     /* Is there enough space to read the count? */
  530.  
  531.     end_ptr = (((char *) Scan) + (2 * (sizeof (format_word))));
  532.     if (end_ptr > ((char *) scan_buffer_top))
  533.     {
  534.       long dw;
  535.  
  536.       extend_scan_buffer (end_ptr, To);
  537.       BCH_START_CLOSURE_RELOCATION (Scan - 1);
  538.       count = (MANIFEST_CLOSURE_COUNT (Scan));
  539.       word_ptr = (FIRST_MANIFEST_CLOSURE_ENTRY (Scan));
  540.       dw = (word_ptr - end_ptr);
  541.       end_ptr = (end_scan_buffer_extension (end_ptr));
  542.       word_ptr = (end_ptr + dw);
  543.       Scan = ((SCHEME_OBJECT *) (end_ptr - (2 * (sizeof (format_word)))));
  544.     }
  545.     else
  546.     {
  547.       BCH_START_CLOSURE_RELOCATION (Scan - 1);
  548.       count = (MANIFEST_CLOSURE_COUNT (Scan));
  549.       word_ptr = (FIRST_MANIFEST_CLOSURE_ENTRY (Scan));
  550.     }
  551.     end_ptr = ((char *) (MANIFEST_CLOSURE_END (Scan, count)));
  552.  
  553.     for ( ; ((--count) >= 0);
  554.          (word_ptr = (NEXT_MANIFEST_CLOSURE_ENTRY (word_ptr))))
  555.     {
  556.       if ((CLOSURE_ENTRY_END (word_ptr)) > ((char *) scan_buffer_top))
  557.       {
  558.         char * entry_end;
  559.         long de, dw;
  560.  
  561.         entry_end = (CLOSURE_ENTRY_END (word_ptr));
  562.         de = (end_ptr - entry_end);
  563.         dw = (entry_end - word_ptr);
  564.         extend_scan_buffer (entry_end, To);
  565.         fasdump_manifest_closure ();
  566.         entry_end = (end_scan_buffer_extension (entry_end));
  567.         word_ptr = (entry_end - dw);
  568.         end_ptr = (entry_end + de);
  569.       }
  570.       else
  571.         fasdump_manifest_closure ();
  572.     }
  573.     Scan = ((SCHEME_OBJECT *) (end_ptr));
  574.     BCH_END_CLOSURE_RELOCATION (Scan);
  575.     break;
  576.       }
  577.  
  578.       case_Cell:
  579.     fasdump_normal_pointer (copy_cell (), 1);
  580.  
  581.       case TC_REFERENCE_TRAP:
  582.     if ((OBJECT_DATUM (Temp)) <= TRAP_MAX_IMMEDIATE)
  583.     {
  584.       /* It is a non pointer. */
  585.       break;
  586.     }
  587.     /* It is a pair, fall through. */
  588.  
  589.       case TC_WEAK_CONS:
  590.       case_Fasdump_Pair:
  591.     fasdump_normal_pointer (copy_pair (), 2);
  592.  
  593.       case TC_INTERNED_SYMBOL:
  594.       {
  595.     fasdump_normal_setup ();
  596.     *To++ = *Old;
  597.     *To++ = BROKEN_HEART_ZERO;
  598.     fasdump_transport_end (2);
  599.     fasdump_normal_end ();
  600.       }
  601.  
  602.       case TC_UNINTERNED_SYMBOL:
  603.       {
  604.     fasdump_normal_setup ();
  605.     *To++ = *Old;
  606.     *To++ = UNBOUND_OBJECT;
  607.     fasdump_transport_end (2);
  608.     fasdump_normal_end ();
  609.       }
  610.  
  611.       case_Triple:
  612.     fasdump_normal_pointer (copy_triple (), 3);
  613.  
  614.       case TC_VARIABLE:
  615.       {
  616.     fasdump_normal_setup ();
  617.     *To++ = *Old;
  618.     *To++ = UNCOMPILED_VARIABLE;
  619.     *To++ = SHARP_F;
  620.     fasdump_transport_end (3);
  621.     fasdump_normal_end ();
  622.       }
  623.  
  624.       case_Quadruple:
  625.     fasdump_normal_pointer (copy_quadruple (), 4);
  626.  
  627.       case TC_BIG_FLONUM:
  628.     fasdump_flonum_setup ();
  629.     goto Move_Vector;
  630.  
  631.       case TC_COMPILED_CODE_BLOCK:
  632.       case_Purify_Vector:
  633.     fasdump_normal_setup ();
  634.       Move_Vector:
  635.     copy_vector (&success);
  636.     if (!success)
  637.     {
  638.       return (PRIM_INTERRUPT);
  639.     }
  640.     fasdump_normal_end ();
  641.  
  642.       case TC_ENVIRONMENT:
  643.     /* Make fasdump fail */
  644.     return (ERR_FASDUMP_ENVIRONMENT);
  645.  
  646.       case TC_FUTURE:
  647.     fasdump_normal_setup ();
  648.     if (!(Future_Spliceable (Temp)))
  649.     {
  650.       goto Move_Vector;
  651.     }
  652.     *Scan = (Future_Value (Temp));
  653.     Scan -= 1;
  654.     continue;
  655.  
  656.       default:
  657.     GC_BAD_TYPE ("dumploop");
  658.     /* Fall Through */
  659.  
  660.       case TC_STACK_ENVIRONMENT:
  661.       case_Fasload_Non_Pointer:
  662.     break;
  663.  
  664.       }
  665.   }
  666.  
  667. end_dumploop:
  668.  
  669.   *To_ptr = To;
  670.   *To_Address_ptr = To_Address;
  671.   return (PRIM_DONE);
  672. }
  673.  
  674. static SCHEME_OBJECT
  675. DEFUN (dump_to_file, (root, fname),
  676.        SCHEME_OBJECT root AND
  677.        char *fname)
  678. {
  679.   Boolean success;
  680.   long value, length, hlength, tlength, tsize;
  681.   SCHEME_OBJECT *dumped_object, *free_buffer, *dummy;
  682.   SCHEME_OBJECT *table_start, *table_end, *table_top;
  683.   SCHEME_OBJECT header[FASL_HEADER_LENGTH];
  684.  
  685.   if (fixup_buffer == ((SCHEME_OBJECT *) NULL))
  686.   {
  687.     fixup_buffer = ((SCHEME_OBJECT *) (malloc (gc_buffer_bytes)));
  688.     if (fixup_buffer == ((SCHEME_OBJECT *) NULL))
  689.       error_system_call (errno, syscall_malloc);
  690.     fixup_buffer_end = (fixup_buffer + gc_buffer_size);
  691.   }
  692.  
  693.   dump_file_name = fname;
  694.   dump_file = (open (dump_file_name, GC_FILE_FLAGS, 0666));
  695.   if (dump_file < 0)
  696.     error_bad_range_arg (2);
  697.  
  698.   compiled_code_present_p = false;
  699.   success = true;
  700.   real_gc_file = (swap_gc_file (dump_file));
  701.   saved_free = Free;
  702.   fixup = fixup_buffer_end;
  703.   fixup_count = -1;
  704.  
  705.   table_top = (&saved_free[Space_Before_GC ()]);
  706.   table_start = (initialize_primitive_table (saved_free, table_top));
  707.   if (table_start >= table_top)
  708.   {
  709.     fasdump_exit (0);
  710.     Primitive_GC (table_start - saved_free);
  711.   }
  712.  
  713.   free_buffer = (initialize_free_buffer ());
  714.   Free = ((SCHEME_OBJECT *) NULL);
  715.   free_buffer += FASL_HEADER_LENGTH;
  716.  
  717.   dummy = free_buffer;
  718.   FLOAT_ALIGN_FREE (Free, dummy);
  719.  
  720.   *free_buffer++ = root;
  721.   dumped_object = Free;
  722.   Free += 1;
  723.  
  724.   value = dumploop (((initialize_scan_buffer ((SCHEME_OBJECT *) NULL))
  725.              + FASL_HEADER_LENGTH),
  726.             &free_buffer, &Free);
  727.   if (value != PRIM_DONE)
  728.   {
  729.     fasdump_exit (0);
  730.     if (value == PRIM_INTERRUPT)
  731.     {
  732.       return (SHARP_F);
  733.     }
  734.     else
  735.     {
  736.       signal_error_from_primitive (value);
  737.     }
  738.   }
  739.   end_transport (&success);
  740.   if (!success)
  741.   {
  742.     fasdump_exit (0);
  743.     return (SHARP_F);
  744.   }
  745.  
  746.   length = (Free - dumped_object);
  747.  
  748.   table_end = (cons_primitive_table (table_start, table_top, &tlength));
  749.   if (table_end >= table_top)
  750.   {
  751.     fasdump_exit (0);
  752.     Primitive_GC (table_end - saved_free);
  753.   }
  754.  
  755.   tsize = (table_end - table_start);
  756.   hlength = ((sizeof (SCHEME_OBJECT)) * tsize);
  757.   if (((lseek (dump_file,
  758.            ((sizeof (SCHEME_OBJECT)) * (length + FASL_HEADER_LENGTH)),
  759.            0))
  760.        == -1)
  761.       || ((write (dump_file, ((char *) &table_start[0]), hlength)) != hlength))
  762.   {
  763.     fasdump_exit (0);
  764.     return (SHARP_F);
  765.   }
  766.  
  767.   hlength = ((sizeof (SCHEME_OBJECT)) * FASL_HEADER_LENGTH);
  768.   prepare_dump_header (header, dumped_object, length, dumped_object,
  769.                0, Constant_Space, tlength, tsize,
  770.                compiled_code_present_p, false);
  771.   if (((lseek (dump_file, 0, 0)) == -1)
  772.       || ((write (dump_file, ((char *) &header[0]), hlength)) != hlength))
  773.   {
  774.     fasdump_exit (0);
  775.     return (SHARP_F);
  776.   }
  777.   return (fasdump_exit (((sizeof (SCHEME_OBJECT)) *
  778.              (length + tsize)) + hlength) ?
  779.       SHARP_T : SHARP_F);
  780. }
  781.  
  782. /* (PRIMITIVE-FASDUMP object-to-dump filename-or-channel flag)
  783.  
  784.    Dump an object into a file so that it can be loaded using
  785.    BINARY-FASLOAD.  A spare heap is required for this operation.  The
  786.    first argument is the object to be dumped.  The second is the
  787.    filename or channel.  The third argument, FLAG, is currently
  788.    ignored.  The primitive returns #T or #F indicating whether it
  789.    successfully dumped the object (it can fail on an object that is
  790.    too large).  It should signal an error rather than return false,
  791.    but ... some other time.
  792.  
  793.    This version of fasdump can only handle files (actually lseek-able
  794.    streams), since the header is written at the beginning of the
  795.    output but its contents are only know after the rest of the output
  796.    has been written.
  797.  
  798.    Thus, for arbitrary channels, a temporary file is allocated, and on
  799.    completion, the file is copied to the channel.
  800.  
  801. */
  802.  
  803. DEFINE_PRIMITIVE ("PRIMITIVE-FASDUMP", Prim_prim_fasdump, 3, 3, 0)
  804. {
  805.   SCHEME_OBJECT root;
  806.   PRIMITIVE_HEADER (3);
  807.  
  808.   root = (ARG_REF (1));
  809.  
  810.   if (STRING_P (ARG_REF (2)))
  811.   {
  812.     PRIMITIVE_RETURN (dump_to_file (root, (STRING_ARG (2))));
  813.   }
  814.   else
  815.   {
  816.     extern char * EXFUN (mktemp, (char *));
  817.     extern int EXFUN (OS_channel_copy,
  818.               (off_t source_length,
  819.                Tchannel source_channel,
  820.                Tchannel destination_channel));
  821.  
  822.     int copy_result;
  823.     SCHEME_OBJECT fasdump_result;
  824.     Tchannel channel, temp_channel;
  825.     char temp_name [19];
  826.     {
  827.       char * scan1 = FASDUMP_FILENAME;
  828.       char * scan2 = temp_name;
  829.       while (1)
  830.     if (((*scan2++) = (*scan1++)) == '\0')
  831.       break;
  832.     }
  833.     channel = (arg_channel (2));
  834.  
  835.     (void) mktemp (temp_name);
  836.     fasdump_result = (dump_to_file (root, (temp_name)));
  837.     if (fasdump_result != SHARP_T)
  838.     {
  839.       PRIMITIVE_RETURN (fasdump_result);
  840.     }
  841.  
  842.     temp_channel = (OS_open_input_file (temp_name));
  843.     copy_result = (OS_channel_copy ((OS_file_length (temp_channel)),
  844.                     temp_channel,
  845.                     channel));
  846.     OS_channel_close (temp_channel);
  847.     OS_file_remove (temp_name);
  848.     if (copy_result < 0)
  849.     {
  850.       signal_error_from_primitive (ERR_IO_ERROR);
  851.     }
  852.     PRIMITIVE_RETURN (SHARP_T);
  853.   }
  854. }
  855.  
  856. /* (DUMP-BAND PROCEDURE FILE-NAME)
  857.    Saves all of the heap and pure space on FILE-NAME.  When the
  858.    file is loaded back using BAND_LOAD, PROCEDURE is called with an
  859.    argument of #F.  */
  860.  
  861. DEFINE_PRIMITIVE ("DUMP-BAND", Prim_band_dump, 2, 2, 0)
  862. {
  863.   extern SCHEME_OBJECT compiler_utilities;
  864.   SCHEME_OBJECT Combination, *table_start, *table_end, *saved_free;
  865.   long table_length;
  866.   Boolean result;
  867.   PRIMITIVE_HEADER (2);
  868.  
  869.   Band_Dump_Permitted ();
  870.   CHECK_ARG (1, INTERPRETER_APPLICABLE_P);
  871.   CHECK_ARG (2, STRING_P);
  872.   Primitive_GC_If_Needed (5);
  873.   saved_free = Free;
  874.   Combination = (MAKE_POINTER_OBJECT (TC_COMBINATION_1, Free));
  875.   Free[COMB_1_FN] = (ARG_REF (1));
  876.   Free[COMB_1_ARG_1] = SHARP_F;
  877.   Free += 2;
  878.   *Free++ = Combination;
  879.   *Free++ = compiler_utilities;
  880.   *Free = (MAKE_POINTER_OBJECT (TC_LIST, (Free - 2)));
  881.   Free++;  /* Some compilers are TOO clever about this and increment Free
  882.           before calculating Free-2! */
  883.   table_start = Free;
  884.   table_end = (cons_whole_primitive_table (Free, Heap_Top, &table_length));
  885.   if (table_end >= Heap_Top)
  886.   {
  887.     result = false;
  888.   }
  889.   else
  890.   {
  891.     CONST char * filename = ((CONST char *) (STRING_LOC ((ARG_REF (2)), 0)));
  892.     dump_channel = (OS_open_dump_file (filename));
  893.     if (dump_channel == NO_CHANNEL)
  894.     {
  895.       error_bad_range_arg (2);
  896.     }
  897.     result = (Write_File ((Free - 1),
  898.               ((long) (Free - Heap_Bottom)), Heap_Bottom,
  899.               ((long) (Free_Constant - Constant_Space)),
  900.               Constant_Space,
  901.               table_start, table_length,
  902.               ((long) (table_end - table_start)),
  903.               (compiler_utilities != SHARP_F), true));
  904.     OS_channel_close_noerror (dump_channel);
  905.     if (!result)
  906.     {
  907.       OS_file_remove (filename);
  908.     }
  909.   }
  910.   Band_Dump_Exit_Hook ();
  911.   Free = saved_free;
  912.   PRIMITIVE_RETURN (BOOLEAN_TO_OBJECT (result));
  913. }
  914.